<?php
/********************************************************************
 *	Author:
 *		Jestery Liu <jestery@gmail.com>
 *		http://www.pte.cn
 *
 *	File:			Filter.class.php
 *	Create Date:	2006-12-31 12:48:22 CST
 *******************************************************************/

Class Filter
{
	private function __construct(){}

	public static function getAlpha($value)
    {
		if (is_array($value))
			return '';
        return preg_replace('/[^[:alpha:]]/', '', $value);
    }

	public static function getAlnum($value)
    {
		if (is_array($value))
			return '';
        return preg_replace('/[^[:alnum:]]/', '', $value);
    }

	public static function getDigits($value)
    {
		if (is_array($value))
			return null;
        return preg_replace('/[^\d]/', '', $value);
    }

	public static function getInt($value)
    {
		if (is_array($value))
			return null;
        return (int) $value;
    }

	public static function isAlnum($value)
    {
        return ctype_alnum($value);
    }

	public static function isAlpha($value)
    {
        return ctype_alpha($value);
    }

	public static function isDigits($value)
    {
        return ctype_digit($value);
    }

	public static function isFloat($value)
    {
		if (is_array($value))
			return false;

        $locale = localeconv();

        $value = str_replace($locale['decimal_point'], '.', $value);
        $value = str_replace($locale['thousands_sep'], '', $value);

        return (strval(floatval($value)) == $value);
    }

	public static function isInt($value)
    {
		if (is_array($value))
			return false;

        $locale = localeconv();

        $value = str_replace($locale['decimal_point'], '.', $value);
        $value = str_replace($locale['thousands_sep'], '', $value);

        return (strval(intval($value)) == $value);
    }

	public static function isBetween($value, $min, $max, $inc = true)
    {
        if ($value > $min &&
            $value < $max) {
            return true;
        }

        if ($value >= $min &&
            $value <= $max &&
            $inc) {
            return true;
        }

        return false;
    }

	public static function isDate($value)
    {
		if (is_array($value))
			return false;

        list($year, $month, $day) = sscanf($value, '%d-%d-%d');

        return checkdate($month, $day, $year);
    }

	public static function isIp($value)
    {
        return (bool) ip2long($value);
    }

	public static function isEmail($email)
	{
		return strlen($email) > 8 && preg_match("/^[-_+.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+([a-z]{2,4})|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email);
	}

	public static function rStripSlashes(&$string, $force = false) 
	{
		if(is_array($string))
		{
			foreach($string as $key => $val)
			{
				self::rStripSlashes($string[$key], $force);
			}
		}
		else
		{
			$string = stripslashes($string);
		}
	}

	public static function rHtmlSpecialchars($string)
	{
		if(is_array($string))
		{
			foreach($string as $key => $val)
				$string[$key] = self::rHtmlSpecialchars($val);
		}
		else
		{
			$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
				str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
		}
		return $string;
	}
}

/* vim: set ts=4 sw=4 ff=unix: */
